﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Dycox.Reporting
{
    public class DataSheetColumn
    {
        public DataSheetColumn()
        {

        }

        private ElementUnit _width;
        [XmlAttribute("width")]
        public ElementUnit Width
        {
            get { return _width; }
            set
            {
                if (!_width.Equals(value))
                {
                    _width = value;
                }
            }
        }

        private void AttachCellSettings(DataSheetCellSettings old, DataSheetCellSettings @new)
        {
            if (old != null) old.Column = null;
            if (@new != null) @new.Column = this;
        }

        private DataSheetHeaderCell _header;
        [XmlElement("header")]
        public DataSheetHeaderCell Header
        {
            get { return _header; }
            set
            {
                if (_header != value)
                {
                    AttachCellSettings(_header, value);
                    _header = value;
                }
            }
        }

        private DataSheetBodyCell _body;
        [XmlElement("body")]
        public DataSheetBodyCell Body
        {
            get { return _body; }
            set
            {
                if (_body != value)
                {
                    AttachCellSettings(_body, value);
                    _body = value;
                }
            }
        }

        private DataSheetFooterCell _footer;
        [XmlElement("footer")]
        public DataSheetFooterCell Footer
        {
            get { return _footer; }
            set
            {
                if (_footer != value)
                {
                    AttachCellSettings(_footer, value);
                    _footer = value;
                }
            }
        }

        internal ElementStyle GetHeaderStyle()
        {
            if (_header != null)
            {
                ElementStyle style = _header.Style;
                if (style == null) style = new ElementStyle { HorizontalAlignment = HorizontalAlignment.Center };
                return style;
            }
            else
                return null;
        }

        internal ElementStyle GetFooterStyle()
        {
            if (_footer != null)
            {
                return _footer.Style ?? _body.Style;
            }
            else
                return null;
        }
    }
}
